const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

app.post('/getallrecdata',(req,res) => {
	var sql = $sql.procure_record.getallrecdata;
	var params = [req.body.account];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
 //根据account和状态获取所有采购记录
app.post('/getrecdata',(req,res) => {
	var sql = $sql.procure_record.getrecdata;
	var params = [req.body.account,req.body.state];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id获取采购申请
app.post('/getrecdatabyid',(req,res) => {
	var sql = $sql.procure_record.getrecdatabyid;
	var params = [req.body.id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新采购记录
app.put('/upprorecord', (req, res) => {
	var sql = $sql.procure_record.uprecorddata;
    var params = [req.body.data,req.body.stoname,req.body.stoid,req.body.num,req.body.stospe,
				 req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,
				 req.body.bank_account,req.body.whentryname,req.body.title,req.body.id,]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id删除采购记录
app.delete('/delrecord',(req,res) => {
	var sql = $sql.procure_record.delrecord;
	var params = req.body.id;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//添加采购记录
app.put('/recordadd', (req, res) => {
	var sql = $sql.procure_record.recordadd;
    var params = [req.body.state,req.body.wname,req.body.data,req.body.stoname,req.body.stoid,req.body.stospe,
				  req.body.num,req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,
				  req.body.bank_account,req.body.whentryname,req.body.title]

	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//获取处于初始状态的id
app.post('/getidbystate',(req,res) => {
	var sql = $sql.procure_record.getidbystate;
	var params = [req.body.state];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新记录状态
app.put('/upstate', (req, res) => {
	var sql = $sql.procure_record.upstate;
    var params = [req.body.state,req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;